package cousin.bean;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

public class MyDataSource implements BeanNameAware , ApplicationContextAware, InitializingBean, DisposableBean {
    private String driver;
    private String url;
    private String username;
    private String password;
    private String beanName;
    private ApplicationContext applicationContext;

    public String getDriver() {
        return driver;
    }

    public void setDriver(String driver) {
        System.out.println("2,执行set构造"+ driver);
        this.driver = driver;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "MyDataSource [driver=" + driver + ", url=" + url + ", username=" + username + ", password=" + password
                + "]";
    }

    public MyDataSource() {
        System.out.println("1,执行无参构造");
    }

    public MyDataSource(String driver, String url, String username, String password) {
        this.driver = driver;
        this.url = url;
        this.username = username;
        this.password = password;
    }
    public void getConnection() {

        System.out.println("正在获取数据库连接...");
        System.out.println("驱动: " + this.driver);
        System.out.println("URL: " + this.url);
        System.out.println("用户名: " + this.username);
        System.out.println("密码: " + this.password);
        System.out.println("成功建立数据库连接!");
    }

    @Override
    public void setBeanName(String s) {
        this.beanName =s;
        System.out.println("3,执行setBeanName"+ s);
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
        System.out.println("36,执行setApplicationContext"+ applicationContext);
    }
    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("5.执行InitializingBean接口规定的初始化方法: afterPropertiesSet");
    }
    public void init() {
        System.out.println("6.执行init方法");
    }
    @Override
    public void destroy() throws Exception {
        System.out.println("9.执行DisposableBean接口规定的销毁方法: destroy");
    }
    public void close() {
        System.out.println("10.执行 自定义的销毁方法 (destroy-method)");
    }
}
